package com.triones.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.leptage.CryptoBillScene;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysOss;
import com.triones.api.entity.CryptoBillDO;
import com.triones.api.entity.KycVerifyDO;
import com.triones.api.entity.param.CryptoBillParams;
import com.triones.api.entity.vo.CryptoBillVO;
import com.triones.api.entity.vo.KycVerifyVO;
import com.triones.api.mapper.CryptoBillMapper;
import com.triones.api.service.CryptoBillService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 加密货币账单 -业务实现
 */
@Service
@RequiredArgsConstructor
public class CryptoBillServiceImpl implements CryptoBillService {

    private final CryptoBillMapper cryptoBillMapper;

    @Override
    public TableDataInfo<CryptoBillVO> pageList(CryptoBillParams cryptoBillParams, PageQuery pageQuery,String type) {

        Map<String, Object> params = cryptoBillParams.getParams();
        LambdaQueryWrapper<CryptoBillDO> lqw = new LambdaQueryWrapper<CryptoBillDO>();
        lqw.eq(StringUtils.isNotEmpty(cryptoBillParams.getStatus()),CryptoBillDO::getStatus,cryptoBillParams.getStatus())
            .eq(StringUtils.isNotEmpty(cryptoBillParams.getTargetCcy()),CryptoBillDO::getTargetCcy,cryptoBillParams.getTargetCcy())
            .eq(StringUtils.isNotEmpty(cryptoBillParams.getTargetChain()),CryptoBillDO::getTargetChain,cryptoBillParams.getTargetChain())
            .eq(StringUtils.isNotEmpty(cryptoBillParams.getTargetCcy()),CryptoBillDO::getTargetCcy,cryptoBillParams.getTargetCcy())
            .eq(StringUtils.isNotEmpty(cryptoBillParams.getTargetChain()),CryptoBillDO::getTargetChain,cryptoBillParams.getTargetChain())
            .and(StringUtils.isNotEmpty(cryptoBillParams.getKeyword()),
                w -> w.like(CryptoBillDO::getTargetAccount,cryptoBillParams.getKeyword()).or()
                .like(CryptoBillDO::getTransferAccount,cryptoBillParams.getKeyword())
                .like(CryptoBillDO::getSellAmount,cryptoBillParams.getKeyword())
                .like(CryptoBillDO::getBuyAmount,cryptoBillParams.getKeyword())
            );
        if ("transfer".equals(type)) {
            if (cryptoBillParams.getStatus() == null) {
                lqw.in(CryptoBillDO::getScene, CryptoBillScene.TRANSFER_IN.getCode(), CryptoBillScene.TRANSFER_OUT.getCode());
            } else {
                lqw.eq(CryptoBillDO::getStatus,cryptoBillParams);
            }
        }
        if ("exchange".equals(type)) {
            if (cryptoBillParams.getStatus() == null) {
                lqw.in(CryptoBillDO::getScene, CryptoBillScene.TRANSFER_CRYPTO.getCode(), CryptoBillScene.TRANSFER_FIAT.getCode());
            } else {
                lqw.eq(CryptoBillDO::getStatus,cryptoBillParams);
            }
        }
        lqw.between(params.get("createTime") != null && params.get("createTime") != null,
            CryptoBillDO::getCreateTime, params.get("createTime"), params.get("createTime"))
            .orderByDesc(CryptoBillDO::getCreateTime);
        Page<CryptoBillVO> result = cryptoBillMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public CryptoBillVO getInfo(Long id) {
        return cryptoBillMapper.selectVoById(id);
    }
}
